package com.xuelang.pipeline.infrastructure.pipeline.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.ChangeMgmtQuery;
import com.xuelang.pipeline.domain.pipeline.ChangeMgmt;
import com.xuelang.pipeline.domain.pipeline.repository.ChangeMgmtRepository;
import com.xuelang.pipeline.infrastructure.pipeline.converter.ChangeMgmtToDOConverter;
import com.xuelang.pipeline.infrastructure.pipeline.dataobject.ChangeMgmtDO;
import com.xuelang.pipeline.infrastructure.pipeline.mapper.ChangeMgmtMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>组件变更管理仓库实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-08-02 11:42
 **/
@Slf4j
@Service
public class ChangeMgmtRepositoryImpl implements ChangeMgmtRepository {

    @Autowired
    private ChangeMgmtMapper mapper;

    @Override
    public PageRecord<ChangeMgmt> page(PageParam<ChangeMgmtQuery> query) {
        Long currentPage = Objects.isNull(query.getCurrentPage()) ? 1L : query.getCurrentPage();
        Long pageSize = Objects.isNull(query.getPageSize()) ? 10L : query.getPageSize();
        ChangeMgmtQuery condition = query.getCondition();
        Page<ChangeMgmtDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<ChangeMgmtDO> wrapper = Wrappers.lambdaQuery(ChangeMgmtDO.class);
        wrapper.eq(StringUtils.isNotBlank(condition.getComponentLabel()), ChangeMgmtDO::getComponentLabel, condition.getComponentLabel());

        Page<ChangeMgmtDO> pageResult = mapper.selectPage(page, wrapper);
        if (Objects.nonNull(pageResult)) {
            return new PageRecord<>(
                    page.getCurrent(),
                    page.getSize(),
                    pageResult.getTotal(),
                    pageResult.getPages(),
                    page.getRecords().stream().map(ChangeMgmtToDOConverter.INSTANCE::fromDO).collect(Collectors.toList())
            );
        }
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                0L,
                0L,
                null
        );
    }

    @Override
    public Boolean add(ChangeMgmt toAdd) {
        return mapper.insert(ChangeMgmtToDOConverter.INSTANCE.toDO(toAdd)) == 1;
    }

    @Override
    public Boolean update(ChangeMgmt toUpdate) {
        return mapper.updateById(ChangeMgmtToDOConverter.INSTANCE.toDO(toUpdate)) == 1;
    }

    @Override
    public ChangeMgmt queryById(String id) {
        return ChangeMgmtToDOConverter.INSTANCE.fromDO(mapper.selectById(id));
    }

    @Override
    public Boolean removeById(String id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public Boolean removeByComponentLabel(String componentLabel) {
        LambdaQueryWrapper<ChangeMgmtDO> wrapper = Wrappers.lambdaQuery(ChangeMgmtDO.class);
        wrapper.eq(ChangeMgmtDO::getComponentLabel, componentLabel);
        return mapper.delete(wrapper) == 1;
    }
}
